package com.google.gwt.livinglife.client;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TopBar {
	private DockPanel topPanel;
	private HorizontalPanel logoPanel;
	private VerticalPanel signinPanel;
	private Label loginLabel;
	private Anchor signInLink;
	private Anchor signOutLink;
	private UserData userData;

	public TopBar(UserData userData) {
		topPanel = new DockPanel();
		logoPanel = new HorizontalPanel();
		signinPanel = new VerticalPanel();
		loginLabel = new Label(
			"Please sign in to your Google Account to access personal page");
		signInLink = new Anchor("Sign In");
		signOutLink = new Anchor("Sign Out");
		this.userData = userData; 
	}

	public DockPanel getTopPanel() {
		return topPanel;
	}

	public void setTopPanel(DockPanel topPanel) {
		this.topPanel = topPanel;
	}

	public HorizontalPanel getLogoPanel() {
		return logoPanel;
	}

	public void setLogoPanel(HorizontalPanel logoPanel) {
		this.logoPanel = logoPanel;
	}

	public VerticalPanel getSigninPanel() {
		return signinPanel;
	}

	public void setSigninPanel(VerticalPanel signinPanel) {
		this.signinPanel = signinPanel;
	}

	public Label getLoginLabel() {
		return loginLabel;
	}

	public void setLoginLabel(Label loginLabel) {
		this.loginLabel = loginLabel;
	}

	public Anchor getSignInLink() {
		return signInLink;
	}

	public void setSignInLink(Anchor signInLink) {
		this.signInLink = signInLink;
	}

	public Anchor getSignOutLink() {
		return signOutLink;
	}

	public void setSignOutLink(Anchor signOutLink) {
		this.signOutLink = signOutLink;
	}

	void assembleTopPanel(LivingLife livingLife) {
		topPanel.add(getLogoPanel(), DockPanel.WEST);
		topPanel.add(getSigninPanel(), DockPanel.EAST);
		signinPanel.setWidth("400px");
		if (!userData.getLoginInfo().isLoggedIn()) {
			signInLink.setHref(userData.getLoginInfo().getLoginUrl());
			signinPanel.add(getLoginLabel());
			signinPanel.add(getSignInLink());
		} else {
			InlineHTML welcomeText = new InlineHTML("<h3> Welcome " + LivingLife.getLogin_info().getUser_data().getNickname() + "</h3><br>");
			signinPanel.add(welcomeText);
			signOutLink.setHref(livingLife.userData.getLoginInfo().getLogoutUrl());
			signOutLink.setText("Sign Out");
			signinPanel.add(getSignOutLink());
			signinPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		}
		final Image logoImage = new Image("images/logo.png");
		logoPanel.add(logoImage);
		logoPanel.setWidth("500px");
	}
}